package com.geek.javase.ch17.work.tcpwork.tcp7;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/*
 *@Date 2025/11/4
 *@Author wangpeng
 *@Description TODO
 */
public class WriteThread implements Runnable {
    private Socket socket;
    public WriteThread(Socket socket) {
        this.socket = socket;
    }
    @Override
    public void run() {
        try (
                Scanner sc = new Scanner(System.in);
                PrintWriter pw = new PrintWriter(socket.getOutputStream());
        ) {
            while (true) {
                System.out.println("说：");
                String str = sc.nextLine();
                pw.println(str);
                pw.flush();

                if (str.equals("exit")) {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
